package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Author: Administrator
 * CreateTime: 2024/9/22
 * Project: heima-leadnews
 */
@Service
@Transactional
@Slf4j
@RequiredArgsConstructor
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    private final WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmChannelPageReqDto dto) {
        // 1.参数校验
        dto.checkParam();

        // 2.创建分页对象
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page);

        // 3.返回结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult add(WmChannel wmChannel) {
        if (wmChannel == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        WmChannel channel = getById(id);
        if (channel == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        // 1. 参数校验
        if (wmChannel == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        WmChannel channel = getById(wmChannel.getId());
        if (channel == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        wmChannelMapper.updateById(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
